package com.squirrel.michale.test03;

/**
 * @author guanhao 观浩
 * @version v1.0.0.0
 * @createTime 2023/4/14 18:39
 * @company Michale Squirrel
 * @link
 * @description
 */
public class Exam01 {

    public int getCorrectSubstrNum(String inputStr) {
        int length = inputStr.length();
        int curCount = 0;
        int lastCount = 0;
        int result = 0;
        Character currentNumber = inputStr.charAt(0);
        for (int i = 0; i < length; i++) {
            if (inputStr.charAt(i) == currentNumber) {
                ++curCount;
            } else {
                result += Math.min(curCount, lastCount);
                lastCount = curCount;
                currentNumber = inputStr.charAt(i);
                curCount = 1;
            }
        }
        result += Math.min(curCount, lastCount);
        return result;
    }


    public int getCorrectSubstrNum2(String inputStr) {
        int length = inputStr.length();
        int cur = 0;
        int last = 0;
        int sum = 0;
        Character ch = inputStr.charAt(0);
        for (int i = 0; i < length; i++) {
            if (inputStr.charAt(i) == ch) {
                ++cur;
            }else {
                sum += Math.min(cur, last);
                last = cur;
                ch = inputStr.charAt(i);
                cur = 1;
            }
        }
        sum += Math.min(cur, last);
        return sum;
    }

    public static void main(String[] args) {
        Exam01 exam01 = new Exam01();
        String inputStr = "010011100001";
//        输出：8
        System.out.println(exam01.getCorrectSubstrNum(inputStr));
    }
}
